package com.zlcx.tz.live.processor.impl;

import com.zlcx.tz.live.client.SocketClient;
import com.zlcx.tz.live.domain.TimeLearnRoomUsersDO;
import com.zlcx.tz.live.processor.QuitRoomPostProcessor;
import com.zlcx.tz.live.service.impl.UserService;
import com.zlcx.tz.live.vo.ShareMsgParamVO;
import com.zlcx.tz.live.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import static com.zlcx.tz.live.enums.MessageType.TIME_LEARN_ROOM_EXIT;

/**
 * @author: anjl
 * @description: （异步）退出房间发送消息
 * @data: 2020-09-27
 **/
@Slf4j
@Order(100)
@Component
public class QuitRoomMessagePostProcessor implements QuitRoomPostProcessor<TimeLearnRoomUsersDO> {

    @Override
    public void processor(TimeLearnRoomUsersDO roomUser) {

        Asserts.notNull(roomUser, "roomUser is null !");
        asyncTaskExecutor.execute(() -> {
            ShareMsgParamVO msg = buildMessage(roomUser);
            socketClient.sendMsg(msg);
            log.info("计时房间操作-退出房间通知 msg = {}", msg);
        });
    }

    private ShareMsgParamVO buildMessage(TimeLearnRoomUsersDO roomUser) {
        UserVO userInfo = userService.getUserInfo(roomUser.getUserId());
        return ShareMsgParamVO
                .builder()
                .type(TIME_LEARN_ROOM_EXIT.getCode())
                .message(userInfo.getName() + "离开教室")
                .toId(roomUser.getRoomId().toString())
                .fromUserId(roomUser.getUserId().toString())
                .contentId(roomUser.getRoomId().toString())
                .build();
    }

    @Autowired
    private SocketClient socketClient;
    @Autowired
    private UserService userService;
    @Resource(name="taskExecutor")
    AsyncTaskExecutor asyncTaskExecutor;
}
